package com.wodogs.utils;

import java.util.List;

import org.apache.log4j.Logger;

import com.wodogs.utils.bean.WfProxy;
import com.wodogs.utils.service.AccountService;
import com.wodogs.utils.service.ListData;
import com.wodogs.utils.service.ProxyService;
import com.wodogs.utils.tools.WriteLog;

public class BaseProcess {
	
	private Logger logger = Logger.getLogger(this.getClass().getName());  

	protected String web_site;//网站的id
	
	protected ListData listData = new ListData();
//	private List<WfAccount> accountList;//账号列表
//	private List<String> proxyIdList;//查询出的代理的id列表
//	private List<WfProxy> proxyUnusedList;//查询的代理的列表中暂时未用的
//	private List<WfProxy> proxyList;//根据账号列表*10获取的代理列表
	private String proxyTestUrl;//代理测试的url
	private String proxyWebSite;//代理对应的网站
	
	public void go() throws InterruptedException{
		start();//开始
		
		setAccountList();//获取可以用的账号的列表
		
		//如果账号列表为空就直接返回
		if(getListData().getAccountList().size()<1){
//			BaiduManage.test2(null);
			return;
		}
		
		setProxys();//设置代理
		
		setBaseData();
		
		logger.info(getWeb_site()+" commnet threads start");
		
		BaseThread t1 = getThread();
		t1.start();
		while(true){
			if(t1.getState().equals(Thread.State.TERMINATED)){
				logger.info(getWeb_site()+" commnet threads end");
//				BaiduManage.test2(null);
				break;
			}
			Thread.sleep(1000);
		}
		
	}
	
	public void setBaseData(){
		
	}
	
//	public MercuryThread getThread(){
//		MercuryThread t1 = new MercuryThread(accountList, proxyList, proxyIdList, proxyUnusedList);
//		return t1;
//	}
	
	public BaseThread getThread(){
		BaseThread t1 = new BaseThread(listData);
		return t1;
	}
	
	public BaseRunnable getRunnable(){
		BaseRunnable t1 = new BaseRunnable(listData);
		return t1;
	}
	
	/**
	 * 开始的一些log和打开baidu进行网络测试
	 * @throws InterruptedException
	 */
	public void start() throws InterruptedException{
		WriteLog.write("mercury "+getWeb_site()+" common start!");
		logger.info("mercury "+getWeb_site()+" common start!");
//		BaiduManage.test1(null);
	}

	/**
	 * 设置账号列表
	 * @param status
	 */
	public void setAccountList(){
		listData.setAccountList(AccountService.getResetList(getWeb_site(),1));
	}

	/**
	 * 设置代理
	 */
	public void setProxys(){
		try{
			String last_proxy = AccountService.getLashProxy(listData.getAccountList());
			List<WfProxy> proxyListLast = getOkList(last_proxy);//ProxyService.getOkList(proxyWebSite,proxyTestUrl,last_proxy,listData.getAccountList().size()*10);
			listData.proxyList = getOkList(null);//ProxyService.getOkList(proxyWebSite,proxyTestUrl,null,listData.getAccountList().size()*10);
			if(proxyListLast!=null){
				listData.proxyList.addAll(proxyListLast);
			}
			int count = 0;
			while(count<50&&(listData.proxyList == null||listData.proxyList.size()<listData.getAccountList().size())){
				List<WfProxy> proxyList2 = getOkList(last_proxy);//ProxyService.getOkList(proxyWebSite,proxyTestUrl,last_proxy,listData.getAccountList().size()*10);
				if(listData.proxyList == null){
					listData.proxyList = proxyList2;
				}else if(proxyList2!=null){
					listData.proxyList.addAll(proxyList2);
				}
				count++;
			}
			listData.proxyIdList = ProxyService.getIdList(listData.proxyList);
			listData.proxyUnusedList = ProxyService.getUnusedList(listData.proxyList);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public List<WfProxy> getOkList(String last_proxy){
		List<WfProxy> proxyListLast = ProxyService.getOkList(getProxyWebSite(),getProxyTestUrl(),null,last_proxy, listData.getAccountList().size()*10);
		return proxyListLast;
	}
	
	public String getWeb_site() {
		return web_site;
	}

	public void setWeb_site(String web_site) {
		this.web_site = web_site;
	}

	public String getProxyTestUrl() {
		return proxyTestUrl;
	}

	public void setProxyTestUrl(String proxyTestUrl) {
		this.proxyTestUrl = proxyTestUrl;
	}

	public String getProxyWebSite() {
		return proxyWebSite;
	}

	public void setProxyWebSite(String proxyWebSite) {
		this.proxyWebSite = proxyWebSite;
	}

	public ListData getListData() {
		return listData;
	}

	public void setListData(ListData listData) {
		this.listData = listData;
	}
	
	
}

